<?php

namespace app\admin\controller;
use think\Db;
use app\admin\model\UserModel;
use app\admin\model\UserType;
use think\Session;

class Leader extends Base
{

    /**
     * [index 用户列表]
     * @return [type] [description]
     * @author
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['a.dai_name|dai_phone'] = ['like',"%" . $key . "%"];
            }
//            if(isset($level)&&$level!="")
//            {
//                $map['a.fen_level'] = $level;
//            }

            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_daili')->alias('a')
                ->join('up_area area','a.dai_shi=area.district','LEFT')
                ->where($map)
                ->count();
            $lists = Db::table('up_daili')->alias('a')
                ->join('up_area area','a.dai_shi=area.district_id','LEFT')
                ->where($map)
                ->order('dai_id desc')
                ->page($Nowpage, $limits)
                ->select();//计算总页面
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch("leader/index");
    }


    /**
     * [userAdd 添加用户]
     * @return [type] [description]
     * @author
     */
    public function fenxiaoAdd()
    {
        if(request()->isPost()){
            $param = input('post.');
            if(!preg_match("/^1[3456789]\d{9}$/", $param['dai_phone'])){
                return json(['status'=>0,'msg'=>'请填写正确的手机号码']);
            }
            $have = Db::table('up_daili')
                ->where('dai_shi',$param['dai_shi'])
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '此区域已经有代理了']);
            }
            $param['dai_time'] = date('Y-m-d H:i:s');
            $res2 = Db::table('up_daili')->insert($param);
            if($res2){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败，请刷新重试']);
            }
        }
        $citys = Db::table('up_area')->where('level',2)->select();
        $this->assign('citys',$citys);
        return $this->fetch('leader/useradd');
    }

    /**
     * [userEdit 编辑用户]
     * @return [type] [description]
     * @author
     */
    public function userEdit()
    {
        if(request()->isPost()){
            $param = input('post.');
            $have = Db::table('up_daili')
                ->where('dai_id','<>',$param['dai_id'])
                ->where('dai_shi',$param['dai_shi'])
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '当前区域代理已存在']);
            }
            $param['dai_time'] = date('Y-m-d H:i:s');
            $res2 = Db::table('up_daili')->where('dai_id',$param['dai_id'])->update($param);
            if($res2){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败，请刷新重试']);
            }
        }
        $id = input('param.id');
        $user = Db::table('up_daili')->where('dai_id',$id)->find();
        $this->assign('user',$user);
        $citys = Db::table('up_area')->where('level',2)->select();
        $this->assign('citys',$citys);
        return $this->fetch("leader/useredit");
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function activeDel(){
        $active_id = input('id');
        Db::startTrans();
        try{
            Db::table('up_daili')->where('dai_id', $active_id)->delete();
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

}